package org.csu.forum.controller;

import com.auth0.jwt.JWT;
import jdk.nashorn.internal.ir.RuntimeNode;
import org.apache.ibatis.annotations.Delete;
import org.csu.forum.common.CommonResponse;
import org.csu.forum.domain.PO.Account;
import org.csu.forum.domain.PO.Post;
import org.csu.forum.domain.PO.ReplyContent;
import org.csu.forum.domain.PO.Titlepost;
import org.csu.forum.domain.VO.*;
import org.csu.forum.service.AccountService;
import org.csu.forum.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.csu.forum.utils.TokenUtils.token;

@Controller

public class AccountController {

    @Autowired
    AccountService accountService;

    @Autowired
    HttpServletRequest httpServletRequest;


    @ResponseBody
    @PostMapping("/tokens")
    public CommonResponse<Map<String, String>> login(@RequestParam int userid,@RequestParam String password) {
        String token = accountService.getToken(userid, password);
        if (token == null)
            return CommonResponse.createForError("用户名或密码错误，请重试");

        Account account = accountService.getAccount(userid);
        Map<String, String> tokenMap = new HashMap<>();//将token打包到map中进行返回
        tokenMap.put("userid", Integer.toString(userid));
        tokenMap.put("username", account.getUsername());
        tokenMap.put("phonenumber", account.getPhonenumber());
        tokenMap.put("token", token);
        return CommonResponse.createForSuccess(tokenMap, "登陆成功");

    }

    @ResponseBody
    @PostMapping("/users")
    public CommonResponse<Account> register(@RequestBody AccountregisterVO accountregisterVO) {

        accountregisterVO.setParameter();

        if (accountService.isUseridExist(accountregisterVO.getUserid())) {
            return CommonResponse.createForError("注册失败，用户名重复");
        }
        accountService.insertAccount(accountregisterVO);
        return CommonResponse.createForSuccess(accountregisterVO.getAccount(), "注册成功");

    }

    @ResponseBody
    @GetMapping("/users/{userid}")
    public CommonResponse<Account> getAccount(@PathVariable int userid, String action) {

        if (action != null)//过滤到这里只要有action则必定不为空

            return CommonResponse.createForSuccessMessage(accountService.isUseridExist(userid) ? "用户已存在" : "用户不存在");

        else {//获取request的头部token信息

            if (userid == (TokenUtils.getClaimByToken(httpServletRequest.getHeader("token"), "userid")))//如果用户名和请求信息的用户名一致才回复
                return CommonResponse.createForSuccess(accountService.getAccount(userid), "获取用户信息成功");

            return CommonResponse.createForError("无法获取他人的信息!");

        }


    }

    @ResponseBody
    @PutMapping("/users/{userid}")
    public CommonResponse<Account> editAccount(@RequestBody Account account) {


        if (account.getUserid() == (TokenUtils.getClaimByToken(httpServletRequest.getHeader("token"), "userid")))//如果用户名和请求信息的用户名一致才回复
        {
            accountService.updateAccount(account);
            return CommonResponse.createForSuccess(accountService.getAccount(account.getUserid()), "修改用户信息成功");
        }

        return CommonResponse.createForError("无法修改他人的信息!");

    }

    @ResponseBody
    @GetMapping("/users/{userid}/titleposts")
    public CommonResponse<List<TitlepostContentVO>> getTitlepost(@PathVariable int userid) {

        return CommonResponse.createForSuccess(accountService.getTitlepostContentVOList(userid), "查看主题成功");

    }

    @ResponseBody
    @PutMapping("/titleposts/{postid}")
    public CommonResponse<Titlepost> editTitlepost(@RequestBody Titlepost titlepost, @PathVariable int postid) {

        titlepost.setPostid(postid);

        //判断是否已经登录，没有登陆不能修改主题
        if (httpServletRequest.getHeader("token") != null) {
            return CommonResponse.createForSuccess(accountService.updateTitlepost(titlepost), "修改成功");
        }

        return CommonResponse.createForError("请先登录，再进行操作");

    }

    @ResponseBody
    @DeleteMapping("/titleposts/{postid}")
    public CommonResponse<TitlepostContentVO> deleteTitlepost(@PathVariable int postid) {

        if (httpServletRequest.getHeader("token") != null) {
            int userid = TokenUtils.getClaimByToken(httpServletRequest.getHeader("token"), "userid");
            return CommonResponse.createForSuccess(accountService.deleteTitlepost(userid, postid), "删除成功");
        }

        return CommonResponse.createForError("请先登录，再进行操作");

    }

    @ResponseBody
    @GetMapping("/users/{userid}/replies")
    public CommonResponse<List<ReplyCheckVO>> getReply(@PathVariable int userid) {

        System.out.println(accountService.getReplyCheckVOList(userid));
        return CommonResponse.createForSuccess(accountService.getReplyCheckVOList(userid), "查看回复成功");

    }

    @ResponseBody
    @PutMapping("/replies/{replyid}")
    public CommonResponse<ReplyCheckVO> editReply(@RequestBody ReplyContent replyContent,@PathVariable int replyid){

        replyContent.setReplyid(replyid);
        //判断是否已经登录，没有登陆不能修改主题
        if (httpServletRequest.getHeader("token") != null) {
            return CommonResponse.createForSuccess(accountService.updateReplyCheckVO(replyContent), "修改成功");
        }

        return CommonResponse.createForError("请先登录，再进行操作");
    }

    @ResponseBody
    @DeleteMapping("/users/{userid}/replies/{replyid}")
    public  CommonResponse<ReplyCheckVO> deleteReply(@PathVariable int userid,@PathVariable int replyid){

        if (httpServletRequest.getHeader("token") != null) {
            int userid1 = TokenUtils.getClaimByToken(httpServletRequest.getHeader("token"), "userid");
            if(userid == userid1)
            return CommonResponse.createForSuccess(accountService.deleteReplyCheckVO(userid,replyid), "删除成功");
            else
            return CommonResponse.createForError("请先登录，再进行操作");
        }
        return CommonResponse.createForError("请先登录，再进行操作");
    }

    @ResponseBody
    @GetMapping("/users/{userid}/collectposts")
    public CommonResponse<List<CollectpostContentVO>> getCollect(@PathVariable int userid){

        if (httpServletRequest.getHeader("token") != null) {

            int userid1 = TokenUtils.getClaimByToken(httpServletRequest.getHeader("token"), "userid");
            if(userid == userid1)
            {
                return CommonResponse.createForSuccess(accountService.getCollectpostContentVOList(userid), "查看收藏成功");
            }
            return CommonResponse.createForError("无法查看别人的收藏");
        }
        return CommonResponse.createForError("请先登录，再进行操作");
    }

    @ResponseBody
    @DeleteMapping("/users/{userid}/collectposts/{collectpostid}")
    public CommonResponse<CollectpostContentVO> deleteCollect(@PathVariable int userid, @PathVariable int collectpostid){

        if (httpServletRequest.getHeader("token") != null) {
            int userid1 = TokenUtils.getClaimByToken(httpServletRequest.getHeader("token"), "userid");
            if(userid == userid1)
                return CommonResponse.createForSuccess(accountService.deleteCollectpost(userid,collectpostid), "删除成功");
            else
                return CommonResponse.createForError("请先登录，再进行操作");
        }
        return CommonResponse.createForError("请先登录，再进行操作");

    }

    @ResponseBody
    @GetMapping("/users/{userid}/posts")
    public CommonResponse<List<AllPostContentVO>> getAllPost(@PathVariable int userid){

        return CommonResponse.createForSuccess(accountService.getAllPostContentVOList(userid), "查看帖子内容成功");
    }

    @ResponseBody
    @DeleteMapping("/posts/{postid}")
    public CommonResponse<PostEditVO> deleteAllPost(@PathVariable int postid){

        if (httpServletRequest.getHeader("token") != null) {
            return CommonResponse.createForSuccess(accountService.deletePostContent(postid),"删除成功");
        }
        return CommonResponse.createForError("请先登录，再进行操作");
    }

    @ResponseBody
    @PutMapping("/posts/{postid}")
    public CommonResponse<PostEditVO> editAllPost(@RequestBody PostEditVO postEditVO , @PathVariable int postid){

        postEditVO.setPostid(postid);
        postEditVO.setFollowpostId(postid);
        postEditVO.setParameter1();
        if (httpServletRequest.getHeader("token") != null) {
            return CommonResponse.createForSuccess(accountService.updatePostContent(postEditVO),"修改成功");
        }
        return CommonResponse.createForError("请先登录，再进行操作");
    }

}

